package com.liu.learning.manual_paper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.learning.manual_paper.mapper.PaperVoMapper;
import com.liu.learning.manual_paper.service.PaperVoService;
import com.liu.learning.manual_paper.vo.PaperVO;
import com.our.learning.infrastructure.constant.ResponseStatusEnum;
import com.our.learning.infrastructure.pojo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PaperVoServiceImpl extends ServiceImpl<PaperVoMapper, PaperVO> implements PaperVoService {
    @Autowired
    private final PaperVoMapper paperVoMapper;

    public PaperVoServiceImpl(PaperVoMapper paperVoMapper) {
        this.paperVoMapper = paperVoMapper;
    }

    @Override
    public Response paperList(Integer page, Integer perPage, Integer id, Integer sumScore) {

        if (id != null) {
            LambdaQueryWrapper<PaperVO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PaperVO::getId,id).eq(PaperVO::getType,1);
            PaperVO paperVO = paperVoMapper.selectOne(lambdaQueryWrapper);
            List<PaperVO> paperVOS = new ArrayList<>();
            if(paperVO == null) return new Response(ResponseStatusEnum.OK).addData("rows",paperVOS).addData("total",0);
            else paperVOS.add(paperVO);
            return new Response(ResponseStatusEnum.OK).addData("rows",paperVOS).addData("total",1);
        }
        LambdaQueryWrapper<PaperVO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if ( sumScore == null||sumScore == 0 ){
            lambdaQueryWrapper.orderByDesc(PaperVO::getId).eq(PaperVO::getType, 1);
        }
        else if(sumScore == 1){
            lambdaQueryWrapper.orderByDesc(PaperVO::getId).eq(PaperVO::getType, 1).isNull(PaperVO::getSumScore);
        }
        else if (sumScore == 2){
            lambdaQueryWrapper.orderByDesc(PaperVO::getId).eq(PaperVO::getType, 1).isNotNull(PaperVO::getSumScore);
        }
        Page<PaperVO> pages = new Page<>(page, perPage);
        page(pages, lambdaQueryWrapper);
        List<PaperVO> list = pages.getRecords();
        return new Response(ResponseStatusEnum.OK).addData("rows",list).addData("total",pages.getTotal());

    }
}
